package com.example.homework7;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class fragment3 extends Fragment {
    public fragment3(){
        super(R.layout.fragment3);


    }

    public void onViewCreated(View view, Bundle saveInstanceState) {
        super.onViewCreated(view, saveInstanceState);
        Button writeInternalButton = getView().findViewById(R.id.writeInternalButton);
        writeInternalButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                try {
                    FileOutputStream os = getActivity().openFileOutput("aaa", Context.MODE_PRIVATE);
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
                    writer.write("Tom");
                    writer.close();
                    Toast.makeText(getActivity(), "写入成功", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        Button readInternalButton = getView().findViewById(R.id.readInternalButton);
        readInternalButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                try{
                    FileInputStream is = getActivity().openFileInput("aaa");
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    String line = reader.readLine();
                    Toast.makeText(getActivity(), line,Toast.LENGTH_SHORT).show();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }
}